{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "4f4985c9",
   "metadata": {},
   "source": [
    "### 实例属性和类属性"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f1fb1a0a",
   "metadata": {},
   "source": [
    "给实例绑定属性的方法是通过实例变量，或者通过self变量："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "3374376c",
   "metadata": {},
   "outputs": [],
   "source": [
    "class Student(object):\n",
    "    def __init__(self,name):\n",
    "        self.name = name\n",
    "s = Student('Bob')\n",
    "s.score = 90"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d76db610",
   "metadata": {},
   "source": [
    "但是，如果Student类本身需要绑定一个属性，可以直接在class中定义属性，这种属性是类属性，归Student类所有："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "dd68fba7",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'Student'"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "class Student(object):\n",
    "    name = 'Student'\n",
    "\n",
    "s = Student()  #创建实例s\n",
    "s.name         #打印name属性，实例并没有这个属性，所以会继续查找class的name属性"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "79201767",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'Student'"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "Student.name"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "09a6b641",
   "metadata": {},
   "outputs": [],
   "source": [
    "s.name = 'Bob'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "fe526e9e",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'Student'"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "Student.name"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "c6b3755c",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'Bob'"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "s.name"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "ab82af41",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'Student'"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "del s.name\n",
    "s.name"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8f979c31",
   "metadata": {},
   "source": [
    "在实际编写程序时，不要对实例属性和类属性使用相同的名字，相同名称的实例属性将屏蔽掉类属性"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "96aafb8c",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
